package com.studyweb.controller;

import com.studyweb.pojo.entity.ShenLunItem;
import com.studyweb.service.ShenLunItemService;
import com.studyweb.pojo.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/shenlun")
@CrossOrigin
public class ShenLunItemController {
    
    @Autowired
    private ShenLunItemService shenLunItemService;
    
    /**
     * 添加申论题目
     */
    @PostMapping
    public Result<String> addShenLunItem(@RequestBody ShenLunItem shenLunItem) {
        try {
            boolean result = shenLunItemService.addShenLunItem(shenLunItem);
            if (result) {
                return Result.success("添加成功");
            } else {
                return Result.error("添加失败");
            }
        } catch (Exception e) {
            return Result.error("添加失败: " + e.getMessage());
        }
    }
    
    /**
     * 删除申论题目
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteShenLunItem(@PathVariable Long id) {
        try {
            boolean result = shenLunItemService.deleteShenLunItem(id);
            if (result) {
                return Result.success("删除成功");
            } else {
                return Result.error("删除失败");
            }
        } catch (Exception e) {
            return Result.error("删除失败: " + e.getMessage());
        }
    }
    
    /**
     * 批量删除申论题目
     */
    @DeleteMapping("/batch")
    public Result<String> deleteShenLunItems(@RequestBody List<Long> ids) {
        try {
            if (ids == null || ids.isEmpty()) {
                return Result.error("请选择要删除的题目");
            }
            
            boolean result = shenLunItemService.deleteShenLunItems(ids);
            if (result) {
                return Result.success("批量删除成功");
            } else {
                return Result.error("批量删除失败");
            }
        } catch (Exception e) {
            return Result.error("批量删除失败: " + e.getMessage());
        }
    }
    
    /**
     * 更新申论题目
     */
    @PutMapping
    public Result<String> updateShenLunItem(@RequestBody ShenLunItem shenLunItem) {
        try {
            boolean result = shenLunItemService.updateShenLunItem(shenLunItem);
            if (result) {
                return Result.success("更新成功");
            } else {
                return Result.error("更新失败");
            }
        } catch (Exception e) {
            return Result.error("更新失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据ID查询申论题目
     */
    @GetMapping("/{id}")
    public Result<ShenLunItem> getShenLunItemById(@PathVariable Long id) {
        try {
            ShenLunItem shenLunItem = shenLunItemService.getShenLunItemById(id);
            if (shenLunItem != null) {
                return Result.success(shenLunItem);
            } else {
                return Result.error("未找到该申论题目");
            }
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }
    
    /**
     * 查询所有申论题目
     */
    @GetMapping
    public Result<List<ShenLunItem>> getAllShenLunItems() {
        try {
            List<ShenLunItem> shenLunItems = shenLunItemService.getAllShenLunItems();
            return Result.success(shenLunItems);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据条件查询申论题目
     */
    @GetMapping("/search")
    public Result<List<ShenLunItem>> searchShenLunItems(
            @RequestParam(required = false) String category,
            @RequestParam(required = false) String subCategory) {
        try {
            // 处理空字符串情况，将其转换为null
            if (category != null && category.isEmpty()) {
                category = null;
            }
            if (subCategory != null && subCategory.isEmpty()) {
                subCategory = null;
            }
            
            List<ShenLunItem> shenLunItems = shenLunItemService.getShenLunItemsByCondition(category, subCategory);
            return Result.success(shenLunItems);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }
}